package cn.kxm.sysInterceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSON;

import cn.kxm.pojo.*;
import cn.kxm.service.PowerService;



public class SysInterceptor extends HandlerInterceptorAdapter {
	//private Logger logger = Logger.getLogger(SysInterceptor.class);
	
	@Autowired
	private PowerService ps;
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception{
		 HttpSession session = request.getSession();
		 Sysuser user=(Sysuser) session.getAttribute("user");
		 StringBuffer url=request.getRequestURL();
		 String check=url.substring(url.indexOf(request.getContextPath()));
		 String XRequested =request.getHeader("X-Requested-With");
         /*if("XMLHttpRequest".equals(XRequested)){*/
        	Model m=ps.checkid(check);
        	if(m!=null){
        		Acl acl=ps.checkmid(m.getId(),user.getRol_id());
        		if(acl!=null){
        			return true;
        		}else{
        			response.setContentType("text/html");  
                    response.setCharacterEncoding("utf-8"); 
                    Map<String, Object>maps=new HashMap<String, Object>();
                    maps.put("result","noPower");
        			response.getWriter().print(JSON.toJSONString(maps));
        		}
        	}else{
        		return true;
        	}
          /*}else{
        	 return true;
          }*/
		 return false;
	}
}
